/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.indicator;

import com.to.aboomy.banner.Indicator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * 横线
 *
 * @since 2021-08-06
 */
public class LineIndicatorView extends DirectionalLayout implements Indicator {
    private static final int NUM_2 = 2;
    private static final int NUM_6 = 6;
    private static final int NUM_8 = 8;
    private static final int NUM_10 = 10;
    private static final int NUM_20 = 20;
    private static final int NUM_300 = 300;
    private static final int FF40C4FF = 0XFF40C4FF;
    private static final int FFCCCCCC = 0xFFCCCCCC;
    DependentLayout.LayoutConfig params = null;
    private Context context;
    private int pointRadius = NUM_10;
    private int pointWidth = pointRadius * NUM_6;
    private int pointHeight = pointRadius * NUM_2;
    private DirectionalLayout directionalLayout;
    private int pagerCount;
    private boolean isSquare;

    /**
     * 构造
     *
     * @param context
     */
    public LineIndicatorView(Context context) {
        super(context);
        this.context = context;
        setOrientation(DirectionalLayout.HORIZONTAL);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(FFCCCCCC));
        setBackground(shapeElement);
    }

    /**
     * 构造
     *
     * @param context
     * @param attrSet
     */
    public LineIndicatorView(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    /**
     * 构造
     *
     * @param context
     * @param attrSet
     * @param styleName
     */
    public LineIndicatorView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    @Override
    public void initIndicatorCount(int pagerCount1) {
        isSquare = true;
        this.pagerCount = pagerCount1;
        setVisibility(pagerCount1 > 1 ? VISIBLE : INVISIBLE);
        removeAllComponents();
        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(
                pagerCount1 * pointWidth, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        directionalLayout = new DirectionalLayout(getContext());
        directionalLayout.setOrientation(DirectionalLayout.HORIZONTAL);
        directionalLayout.setLayoutConfig(lp);

        // 翻页指示的点
        Text pointView = new Text(getContext());
        ShapeElement drawable = new ShapeElement();
        pointView.setWidth(pointWidth);
        drawable.setRgbColor(RgbColor.fromArgbInt(FF40C4FF));
        drawable.setCornerRadius(NUM_8);
        pointView.setSelected(false);
        pointView.setHeight(pointHeight);
        pointView.setBackground(drawable);
        directionalLayout.addComponent(pointView);
        addComponent(directionalLayout);
    }

    @Override
    public Component getView() {
        return this;
    }

    @Override
    public DependentLayout.LayoutConfig getParams() {
        if (params == null) {
            params = new DependentLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, NUM_20);
            params.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_BOTTOM);
            params.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
            params.addRule(DependentLayout.LayoutConfig.BELOW);
            params.setMarginBottom(AttrHelper.vp2px(NUM_10, context));
        }
        return params;
    }

    @Override
    public void setAutoStau(Boolean isAuto) {
    }

    @Override
    public void onPageSliding(int position, float positionOffset, int positionOffsetPixels) {
        if (positionOffsetPixels > 0) {
            isSquare = true;
        } else {
            isSquare = false;
        }
    }

    @Override
    public void onPageSlideStateChanged(int i) {
    }

    @Override
    public void onPageChosen(int position) {
        selectPoint(position);
    }

    /**
     * selectPoint
     *
     * @param position position
     */
    public void selectPoint(int position) {
        final Text pointView = (Text) directionalLayout.getComponentAt(0);
        if (position >= 0 && position < pagerCount) {
            selectPoint(pointView, position);
        }
    }

    private void selectPoint(final Text v, int position) {
        if (v != null) {
            if(isSquare){
                final float temp = pointWidth;
                AnimatorValue animatorValue = new AnimatorValue();
                animatorValue.setDuration(NUM_300);
                animatorValue.setValueUpdateListener((animatorValue1, v1) -> {
                    // 等于当前时间减去动画开始的时间。
                    int startx = pointWidth * (position - 1);
                    float straX = startx + (v1 * temp);
                    v.setTranslationX(straX);
                });
                animatorValue.start();
            } else {
                final float temp = pointWidth;
                AnimatorValue animatorValue = new AnimatorValue();
                animatorValue.setDuration(NUM_300);
                animatorValue.setValueUpdateListener((animatorValue1, v1) -> {
                    // 等于当前时间减去动画开始的时间。
                    int startx = pointWidth * (position+1);
                        float straX = startx - (v1 * temp);
                        v.setTranslationX(straX);
                });
                animatorValue.start();
            }


        }
    }

    private void selectPoint2(final Text v) {
        if (v != null && !v.isSelected()) {
            v.setTranslationX(0);
        }
    }
}
